//
//  TimeUtils.m
//  Molian
//
//  Created by wan song on 16/12/14.
//  Copyright (c) 2014 memeda. All rights reserved.
//

#import "TimeUtils.h"

@implementation TimeUtils

+ (NSString*)getPastTimeWithDate:(NSDate*)date {
    NSDate * d = date;
    NSTimeInterval late = [d timeIntervalSince1970]*1;
    NSString * timeString = nil;
    NSDate * dat = [NSDate dateWithTimeIntervalSinceNow:0];
    NSTimeInterval now = [dat timeIntervalSince1970]*1;
    NSTimeInterval cha = now - late;
    if (cha/3600 < 1) {
        timeString = [NSString stringWithFormat:@"%f", cha/60];
        timeString = [timeString substringToIndex:timeString.length-7];
        int num= [timeString intValue];
        if (num <= 1) {
            timeString = [NSString stringWithFormat:@"刚刚"];
        }else{
            timeString = [NSString stringWithFormat:@"%@分钟前", timeString];
        }
    }
    if (cha/3600 > 1 && cha/86400 < 1) {
        timeString = [NSString stringWithFormat:@"%f", cha/3600];
        timeString = [timeString substringToIndex:timeString.length-7];
        timeString = [NSString stringWithFormat:@"%@小时前", timeString];
    }
    if (cha/86400 > 1)
    {
        timeString = [NSString stringWithFormat:@"%f", cha/86400];
        timeString = [timeString substringToIndex:timeString.length-7];
        int num = [timeString intValue];
        if (num < 2) {
            timeString = [NSString stringWithFormat:@"昨天"];
        }else if(num == 2){
            timeString = [NSString stringWithFormat:@"前天"];
        }else if (num > 2 && num <7){
            timeString = [NSString stringWithFormat:@"%@天前", timeString];
        }else if (num == 7 ) {
            timeString = [NSString stringWithFormat:@"1周前"];
        }else if(num > 7){
            NSInteger days = num;
            timeString = [NSString stringWithFormat:@"%d天前", (int)days];
        }
    }
    return timeString;
}

+ (NSUInteger)getPastMinuteWithDate:(NSDate*)date {
    // 根据日期计算距今的时间,以分钟计
    double pastSeconds = [[NSDate date]timeIntervalSinceDate:date];
    NSInteger pastMinute = pastSeconds/60;
    return pastMinute;
}

+ (NSUInteger)getAgeFromDate:(NSDate*)date {
    // 从日期计算年龄
    // 当前日期
    NSDateComponents *todayComponents = [[NSCalendar currentCalendar] components:NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear fromDate:[NSDate date]];
    NSInteger thisYear = [todayComponents year];
    NSInteger thisMonth = [todayComponents month];
    NSInteger thisDay = [todayComponents day];
    
    NSDateComponents *pastComponents = [[NSCalendar currentCalendar] components:NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear fromDate:date];
    NSInteger pastYear = [pastComponents year];
    NSInteger pastMonth = [pastComponents month];
    NSInteger pastDay = [pastComponents day];
    
    NSInteger deltaYear = thisYear - pastYear;
    NSInteger deltaMonth = thisMonth - pastMonth;
    NSInteger deltaDay = thisDay- pastDay;
    if (deltaYear < 0) {
        return 0;
    }
    
    if (deltaMonth > 0) {
        return deltaYear + 1;
    } else if (deltaMonth < 0) {
        return deltaYear;
    } else {
        if (deltaDay > 0) {
            return deltaYear + 1;
        } else {
            return deltaYear;
        }
    }
}


+ (NSString*)getConstellationWithDate:(NSDate*)date {
    // 根据日期获得星座
    NSInteger constellationId = [self getConstellationIdWithDate:date];
    return [self getConstellationWithId:constellationId];
}

+ (NSInteger)getConstellationIdWithDate:(NSDate*)date {
    // 根据日期获得星座的系统id
    NSUInteger constellation = 0;
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:@"MM"];
    int i_month=0;
    NSString *theMonth = [dateFormat stringFromDate:date];
    if([[theMonth substringToIndex:0] isEqualToString:@"0"]){
        i_month = [[theMonth substringFromIndex:1] intValue];
    }else{
        i_month = [theMonth intValue];
    }
    
    [dateFormat setDateFormat:@"dd"];
    int i_day=0;
    NSString *theDay = [dateFormat stringFromDate:date];
    if([[theDay substringToIndex:0] isEqualToString:@"0"]){
        i_day = [[theDay substringFromIndex:1] intValue];
    }else{
        i_day = [theDay intValue];
    }
    switch (i_month) {
        case 1:
            if(i_day>=20 && i_day<=31){
                constellation = CONSTELLATION_SHUIPING;
            }
            if(i_day>=1 && i_day<=19){
                constellation = CONSTELLATION_MOJIE;
            }
            break;
        case 2:
            if(i_day>=1 && i_day<=18){
                constellation = CONSTELLATION_SHUIPING;
            }
            if(i_day>=19 && i_day<=31){
                constellation = CONSTELLATION_SHUANGYU;
            }
            break;
        case 3:
            if(i_day>=1 && i_day<=20){
                constellation = CONSTELLATION_SHUANGYU;
            }
            if(i_day>=21 && i_day<=31){
                constellation = CONSTELLATION_BAIYANG;
            }
            break;
        case 4:
            if(i_day>=1 && i_day<=19){
                constellation = CONSTELLATION_BAIYANG;
            }
            if(i_day>=20 && i_day<=31){
                constellation = CONSTELLATION_JINGNIU;
            }
            break;
        case 5:
            if(i_day>=1 && i_day<=20){
                constellation = CONSTELLATION_JINGNIU;
            }
            if(i_day>=21 && i_day<=31){
                constellation = CONSTELLATION_SHUANGZI;
            }
            break;
        case 6:
            if(i_day>=1 && i_day<=21){
                constellation = CONSTELLATION_SHUANGZI;
            }
            if(i_day>=22 && i_day<=31){
                constellation = CONSTELLATION_JUXIE;
            }
            break;
        case 7:
            if(i_day>=1 && i_day<=22){
                constellation = CONSTELLATION_JUXIE;
            }
            if(i_day>=23 && i_day<=31){
                constellation = CONSTELLATION_SHIZI;
            }
            break;
        case 8:
            if(i_day>=1 && i_day<=22){
                constellation = CONSTELLATION_SHIZI;
            }
            if(i_day>=23 && i_day<=31){
                constellation = CONSTELLATION_CHUNV;
            }
            break;
        case 9:
            if(i_day>=1 && i_day<=22){
                constellation = CONSTELLATION_CHUNV;
            }
            if(i_day>=23 && i_day<=31){
                constellation = CONSTELLATION_TIANCHENG;
            }
            break;
        case 10:
            if(i_day>=1 && i_day<=23){
                constellation = CONSTELLATION_TIANCHENG;
            }
            if(i_day>=24 && i_day<=31){
                constellation = CONSTELLATION_TIANXIE;
            }
            break;
        case 11:
            if(i_day>=1 && i_day<=21){
                constellation = CONSTELLATION_TIANXIE;
            }
            if(i_day>=22 && i_day<=31){
                constellation = CONSTELLATION_SHESHOU;
            }
            break;
        case 12:
            if(i_day>=1 && i_day<=21){
                constellation = CONSTELLATION_SHESHOU;
            }
            if(i_day>=21 && i_day<=31){
                constellation = CONSTELLATION_MOJIE;
            }
            break;
    }
    return constellation;
}

+ (NSString*)getConstellationWithId:(NSInteger)constellationId {
    // 根据系统星座id获取星座
    NSString *constellationStr = @"";
    switch (constellationId) {
        case CONSTELLATION_MOJIE:
            constellationStr = @"摩羯座";
            break;
        case CONSTELLATION_SHUIPING:
            constellationStr = @"水瓶座";
            break;
        case CONSTELLATION_SHUANGYU:
            constellationStr = @"双鱼座";
            break;
        case CONSTELLATION_BAIYANG:
            constellationStr = @"白羊座";
            break;
        case CONSTELLATION_JINGNIU:
            constellationStr = @"金牛座";
            break;
        case CONSTELLATION_SHUANGZI:
            constellationStr = @"双子座";
            break;
        case CONSTELLATION_JUXIE:
            constellationStr = @"巨蟹座";
            break;
        case CONSTELLATION_SHIZI:
            constellationStr = @"狮子座";
            break;
        case CONSTELLATION_CHUNV:
            constellationStr = @"处女座";
            break;
        case CONSTELLATION_TIANCHENG:
            constellationStr = @"天秤座";
            break;
        case CONSTELLATION_TIANXIE:
            constellationStr = @"天蝎座";
            break;
        case CONSTELLATION_SHESHOU:
            constellationStr = @"射手座";
            break;
        default:
            break;
    }
    return constellationStr;
    
}

@end
